<?php
// Simple test file to verify tag display functionality
require_once 'config/config.php';
require_once 'includes/database.php';

$db = new Database();
$categories = $db->getCategories();
$tags = [];
$navigations = [];

foreach ($categories as $category) {
    $navigations[$category['id']] = $db->getNavigations($category['id']);
    $tags[$category['id']] = $db->getTags($category['id']);
}

echo "<h1>标签显示功能测试</h1>\n";

foreach ($categories as $category) {
    echo "<h2>分类: " . htmlspecialchars($category['name']) . "</h2>\n";

    if (!empty($navigations[$category['id']])) {
        foreach ($navigations[$category['id']] as $nav) {
            echo "<div style='margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 8px;'>\n";
            echo "<h3>" . htmlspecialchars($nav['name']) . "</h3>\n";
            echo "<p>" . htmlspecialchars($nav['description'] ?? '') . "</p>\n";

            // 显示标签数据
            echo "<div style='margin-top: 10px;'>\n";
            echo "<strong>标签IDs:</strong> " . (empty($nav['tag_ids']) ? '无' : implode(', ', $nav['tag_ids'])) . "<br>\n";

            if (!empty($nav['tag_ids']) && is_array($nav['tag_ids'])) {
                echo "<strong>标签名称:</strong> ";
                $navTags = [];
                foreach ($nav['tag_ids'] as $tagId) {
                    if (!empty($tags[$category['id']])) {
                        foreach ($tags[$category['id']] as $tag) {
                            if ($tag['id'] == $tagId) {
                                $navTags[] = $tag['name_cn'];
                                break;
                            }
                        }
                    }
                }
                echo !empty($navTags) ? implode(', ', $navTags) : '无匹配标签';
                echo "<br>\n";

                // 模拟HTML标签显示
                echo "<div style='margin-top: 8px;'>";
                foreach ($navTags as $tagName) {
                    echo "<span style='display: inline-block; padding: 2px 8px; background: #f0f4ff; border: 1px solid #d0d8ff; border-radius: 12px; font-size: 12px; margin-right: 4px;'>" . htmlspecialchars($tagName) . "</span>";
                }
                echo "</div>";
            }
            echo "</div>\n";
            echo "</div>\n";
        }
    } else {
        echo "<p>该分类下无导航项</p>\n";
    }
    echo "<hr>\n";
}
?>